#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "role.h"
#include "boss.h"
#include"bigboss.h"
#include<QPainter>
#include<qrect.h>
#include<qtimer.h>
#include<QKeyEvent>
#include<QDebug>
#include<QFont>
#include<QPixmap>
#include<QProgressBar>
#include<QRandomGenerator>
#include<QMessageBox>
#include<QDialog>
#include<QTreeWidget>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    GameMap();

    mrole = new Role();
    mrole-> hit_points = 100;
    mrole-> energy = 100;

    mboss1 = new Boss();
    mboss2 = new Boss();
    mboss3 = new Boss();

    mboss1->x = 18;
    mboss1->y = 1;

    mboss2->x = 1;
    mboss2->y = 7;

    mboss3->x = 18;
    mboss3->y = 7;

    mbigboss = new BigBoss(map1, this);
    mbigboss->setPosition(28, 1);

    mrole->energy = 100;

    connect(mrole,&Role::role_up,[this](){
        update_role(1);
    } );
    connect(mrole,&Role::role_down,[this](){
        update_role(2);
    } );
    connect(mrole,&Role::role_right,[this](){
        update_role(3);
    } );
    connect(mrole,&Role::role_left,[this](){
        update_role(4);
    } );

    QRect rect1(50,50,50,50);
    role = rect1;
    QRect rect2(900,450,50,50);
    princess = rect2;
    QRect rect3_1(900,50,50,50);
    boss1 = rect3_1;
    QRect rect3_2(50,350,50,50);
    boss2 = rect3_2;
    QRect rect3_3(900,350,50,50);
    boss3 = rect3_3;
    //绘制玩家

    rolelabel = new QLabel(this);
    rolelabel->move(50,50);
    QPixmap role_1_pic(":/img/imgs/imgs_role/role_left/role_left_1.png");
    rolelabel->setPixmap(role_1_pic);
    rolelabel->setFixedSize(role_1_pic.size());

    //绘制bigboss
    bigbosslabel = new QLabel(this);
    bigbosslabel->move(50*mbigboss->x,50*mbigboss->y);
    QPixmap bigboss_pic(":/img/imgs/imgs_role/role_left/role_left_1.png");
    bigbosslabel->setPixmap(bigboss_pic);
    bigbosslabel->setFixedSize(bigboss_pic.size());

    //mbigboss->findPlayer(mrole->x,mrole->y);

    //test

    //按钮功能实现
    connect(ui->pause_btn,&QPushButton::clicked,this,&MainWindow::pause);



    //血条及体力条显示
    connect(mrole,&Role::hp_changed,[this](int _value){
        ui->HP->setValue(_value);
    });
    connect(mrole,&Role::en_changed,[this](int _value){
        ui->EN->setValue(_value);
    });

    //计时器与敌人移动
    timer = new QTimer(this);
    timer->start(450);//3
    timer_bb = new QTimer(this);
    timer_bb->start(300);//2
    connect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
    connect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);

    //按时间给角色回复体力
    timer_en = new QTimer(this);
    timer_en -> start(900);//6
    connect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);

    //移动动画
    currentFrame = 0;
    moveCooldown = 0;
    role_isAnimating_left = false;
    role_isAnimating_right = false;
    role_isAnimating_up = false;
    role_isAnimating_down = false;
    currentFrame = 0;
    timer_role = new QTimer(this);
    timer_role->start(150);
    connect(timer_role, &QTimer::timeout, this, &MainWindow::update_role_animate);
    //移动冷却
    moveCooldownTimer = new QTimer(this);
    moveCooldownTimer -> start(300);
    connect(moveCooldownTimer, &QTimer::timeout, [this](){
        if(moveCooldown > 0){
            moveCooldown = 0;
        }
    });
    //受伤无敌帧
    hitCooldownTimer = new QTimer(this);
    hitCooldownTimer -> start(2400);
    connect(hitCooldownTimer, &QTimer::timeout, [this](){
        if(hitCooldown > 0){
            hitCooldown = 0;
        }
    });
    //判断人物，怪物及出口的相对位置
    connect(timer_role,&QTimer::timeout,[this](){
        judge_boss(role,boss1);
        judge_boss(role,boss2);
        judge_boss(role,boss3);
        judge_exit(role,princess);
        judge_bigboss(mrole->x,mrole->y,mbigboss->x,mbigboss->y);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
    delete mrole;
    delete mboss1;
    delete mboss2;
    delete mboss3;
    delete timer;
    delete timer_en;
    delete timer_role;
}

void MainWindow::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    //绘制地图边框
    /*
    painter.setPen(Qt::black);
    painter.setBrush(Qt::lightGray);
    painter.drawRect(50,50,1500,1000);
*/

    //绘制墙体
    QPixmap wall(":/img/imgs/wall_1.png");
    QPixmap empty(":/img/imgs/empty.png");

    for(int y = 0;y < 20; y++){
        for(int x = 0;x < 30;x++){
            if(getCell(x, y) == Wall){
                painter.drawPixmap(x*50,y*50,50,50,wall);
            }

            else{
                painter.drawPixmap(x*50,y*50,50,50,empty);
            }


        }
    }
    //绘制公主
    painter.setBrush(Qt::white);
    QPixmap princess_pic(":/img/imgs/door.png");
    painter.drawPixmap(princess,princess_pic);

    //绘制Boss
    painter.setBrush(Qt::white);
    QPixmap boss_down_pic(":/img/imgs/imgs_boss/boss_down.png");
    QPixmap boss_up_pic(":/img/imgs/imgs_boss/boss_up.png");
    QPixmap boss_left_pic(":/img/imgs/imgs_boss/boss_left.png");
    QPixmap boss_right_pic(":/img/imgs/imgs_boss/boss_right.png");
    painter.drawPixmap(boss1,boss_down_pic);
    painter.drawPixmap(boss2,boss_left_pic);
    painter.drawPixmap(boss3,boss_right_pic);
}

void MainWindow::keyPressEvent(QKeyEvent *event){
    if (moveCooldown > 0) return;
    if(mrole->energy >=10 && mrole->energy<=100){
        change_en_to(mrole->energy-10);
        switch(event->key()){
        case Qt::Key_W:{
            role_up_broadcast();
            role_isAnimating_up = true;
            break;
        }
        case Qt::Key_A:{
            role_left_broadcast();
            role_isAnimating_left = true;
            break;
        }
        case Qt::Key_D:{
            role_right_broadcast();
            role_isAnimating_right = true;
            break;
        }
        case Qt::Key_S:{
            role_down_broadcast();
            role_isAnimating_down = true;
            break;
        }
        }
        moveCooldown = 1;
        update();
    }
}


void MainWindow::keyReleaseEvent(QKeyEvent *event){
    switch(event->key()){
    case Qt::Key_W:{
        role_isAnimating_up = false;
        QPixmap role_up_1_pic(":/img/imgs/boss_1.png");
        break;
    }
    case Qt::Key_A:{
        role_isAnimating_left = false;
        currentFrame = 0;
        break;
    }
    case Qt::Key_D:{
        role_isAnimating_right = false;
        QPixmap role_right_1_pic(":/img/imgs/boss_1.png");
        break;
    }
    case Qt::Key_S:{
        role_isAnimating_down = false;
        QPixmap role_down_1_pic(":/img/imgs/boss_1.png");
        break;
    }
    }
    update();
    /*
    if (!role_isAnimating_up && !role_isAnimating_left && !role_isAnimating_right && !role_isAnimating_down) {
        // 停止动画
        timer_role->stop();
    }
    */
}

void MainWindow::mousePressEvent(QMouseEvent *event){

}

void MainWindow::mouseReleaseEvent(QMouseEvent *event){

}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event){

}

void MainWindow::mouseMoveEvent(QMouseEvent *event){

}


void MainWindow::update_role(int Direction){
    switch(Direction){
    case 1:{
        //w
        if(getCell(mrole->x, mrole->y - 1)==Wall){break;}
        mrole->y -= 1;
        role.setBottom(mrole->y*50-50);
        role.setTop(mrole->y*50);
        rolelabel->move(mrole->x*50,mrole->y*50);
        break;
    }
    case 2:{
        //s
        if(getCell(mrole->x, mrole->y + 1)==Wall){break;}
        mrole->y += 1;
        role.setBottom(mrole->y*50-50);
        role.setTop(mrole->y*50);
        rolelabel->move(mrole->x*50,mrole->y*50);
        break;
    }
    case 3:{
        //d
        if(getCell(mrole->x + 1, mrole->y)==Wall){break;}
        mrole->x += 1;
        role.setRight(mrole->x*50-50);
        role.setLeft(mrole->x*50);
        rolelabel->move(mrole->x*50,mrole->y*50);
        break;
    }
    case 4:{
        //a
        if(getCell(mrole->x - 1, mrole->y)==Wall){break;}
        mrole->x -= 1;
        role.setRight(mrole->x*50-50);
        role.setLeft(mrole->x*50);
        rolelabel->move(mrole->x*50,mrole->y*50);
        break;
    }
    default:;
}
    update();
}

void MainWindow::game_restart(){
    //重置角色状态
    change_hp_to(100);
    change_en_to(100);
    rolelabel->move(50,50);
    QRect rect1(50,50,50,50);
    role = rect1;
    mrole->x = 1;
    mrole->y = 1;
    //重置boss状态
    QRect rect3_1(500,300,50,50);
    boss1 = rect3_1;
    QRect rect3_2(400,400,50,50);
    boss2 = rect3_2;
    QRect rect3_3(600,100,50,50);
    boss3 = rect3_3;
    //重置bb状态
    mbigboss->setPosition(28, 1);
    bigbosslabel->move(50*mbigboss->x,50*mbigboss->y);
    //重置bb路径信息
    resetBigBossPath();
    update();
}

void MainWindow::pause(){
    disconnect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
    disconnect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
    disconnect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);

    auto pause_question = QMessageBox::question(this,"游戏已暂停","是否继续进行游戏？",QMessageBox::Yes|QMessageBox::No);
    if(pause_question == QMessageBox::Yes){
        connect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
        connect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
        connect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);

    }
    else{
        connect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
        connect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
        connect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);
        game_restart();
    }
}

void MainWindow::update_boss(){
    int randomdirection = QRandomGenerator::global()->bounded(4);

    switch(randomdirection){
    case 0:{
        if(boss1.top()==50){break;}
        boss1.setBottom(boss1.bottom()-50);//上
        boss1.setTop(boss1.top()-50);


        if(boss2.left()==1400){break;}
        boss2.setLeft(boss2.left()+50);//右
        boss2.setRight(boss2.right()+50);


        if(boss3.left()==50){break;}
        boss3.setLeft(boss3.left()-50);//左
        boss3.setRight(boss3.right()-50);

        break;
    }
    case 1:{
        if(boss1.top()==900){break;}
        boss1.setBottom(boss1.bottom()+50);//下
        boss1.setTop(boss1.top()+50);


        if(boss2.left()==50){break;}
        boss2.setLeft(boss2.left()-50);//左
        boss2.setRight(boss2.right()-50);


        if(boss3.top()==50){break;}
        boss3.setBottom(boss3.bottom()-50);//上
        boss3.setTop(boss3.top()-50);


        break;
    }
    case 2:{
        if(boss1.left()==1400){break;}
        boss1.setLeft(boss1.left()+50);//右
        boss1.setRight(boss1.right()+50);


        if(boss2.top()==900){break;}
        boss2.setBottom(boss2.bottom()+50);//下
        boss2.setTop(boss2.top()+50);


        if(boss3.top()==900){break;}
        boss3.setBottom(boss3.bottom()+50);//下
        boss3.setTop(boss3.top()+50);


        break;
    }
    case 3:{
        if(boss1.left()==50){break;}
        boss1.setLeft(boss1.left()-50);//左
        boss1.setRight(boss1.right()-50);


        if(boss2.top()==50){break;}
        boss2.setBottom(boss2.bottom()-50);//上
        boss2.setTop(boss2.top()-50);


        if(boss3.left()==1400){break;}
        boss3.setLeft(boss3.left()+50);//右
        boss3.setRight(boss3.right()+50);


        break;
    }
    }
    update();
}

void MainWindow::update_bigBoss(){
    mbigboss->findPlayer(mrole->x, mrole->y);
    bigbosslabel->move(50*mbigboss->x,50*mbigboss->y);
    update();
}

void MainWindow::change_hp_to(int hp){
    if(hp>=0&&hp<=100){
        mrole->hit_points = hp;
        hp_change_broadcast(hp);
    }
    else if(hp > 100){
        mrole->hit_points = 100;
        hp_change_broadcast(hp);
    }
}

void MainWindow::change_en_to(int en){
    if(en>=0&&en<=100){
        mrole->energy = en;
        en_change_broadcast(en);
    }
    else if(en > 100){
        mrole->energy = 100;
        hp_change_broadcast(en);
    }
}

void MainWindow::en_rise(){
    change_en_to(mrole->energy+20);
}

void MainWindow::role_up_broadcast(){
    emit mrole->role_up();
}
void MainWindow::role_down_broadcast(){
    emit mrole->role_down();
}
void MainWindow::role_right_broadcast(){
    emit mrole ->role_right();
}
void MainWindow::role_left_broadcast(){
    emit mrole ->role_left();
}
void MainWindow::hp_change_broadcast(int hp){
    emit mrole ->hp_changed(hp);

}
void MainWindow::en_change_broadcast(int en){
    emit mrole ->en_changed(en);

}

void MainWindow::judge_bigboss(int x1,int y1,int x2,int y2){
    if(x1 == x2 && y1 == y2){
        change_hp_to(0);
        disconnect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
        disconnect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
        disconnect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);
        QDialog dialog(this);
        dialog.resize(200,100);
        QLabel *label1 = new QLabel("你死了，游戏结束", &dialog);
        label1->resize(150,20);
        label1->move(25,10);
        QPushButton *btn1 = new QPushButton("重新开始游戏", &dialog);
        QVBoxLayout *layout = new QVBoxLayout(&dialog);
        layout->addWidget(btn1);
        connect(btn1, &QPushButton::clicked, &dialog, &QDialog::close);
        connect(btn1,&QPushButton::clicked,[this](){
            game_restart();
            connect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
            connect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
            connect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);
        });
        dialog.exec();
    }
    return;
}

void MainWindow::judge_boss(QRect rect1,QRect rect2){
    if(rect1.left() == rect2.left()&&rect1.top() == rect2.top()){
        change_hp_to(mrole->hit_points-50);
        if(mrole->hit_points != 0){
            hitCooldown = 1;
        }
        else{
            disconnect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
            disconnect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
            disconnect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);
            QDialog dialog(this);
            dialog.resize(200,100);
            QLabel *label1 = new QLabel("你死了，游戏结束", &dialog);
            label1->resize(150,20);
            label1->move(25,10);
            QPushButton *btn1 = new QPushButton("重新开始游戏", &dialog);
            QVBoxLayout *layout = new QVBoxLayout(&dialog);
            layout->addWidget(btn1);
            connect(btn1, &QPushButton::clicked, &dialog, &QDialog::close);
            connect(btn1,&QPushButton::clicked,[this](){
                game_restart();
                connect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
                connect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
                connect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);
            });
            dialog.exec();
        }

    }
    return;
}

void MainWindow::judge_exit(QRect rect1,QRect rect2){
    if(rect1.left() == rect2.left()&&rect1.top() == rect2.top()){
        disconnect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
        disconnect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
        disconnect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);
        QDialog dialog(this);
        dialog.resize(200,100);
        QLabel *label1 = new QLabel("恭喜你，成功逃出", &dialog);
        label1->resize(150,20);
        label1->move(25,10);
        QPushButton *btn1 = new QPushButton("重新开始游戏", &dialog);
        QVBoxLayout *layout = new QVBoxLayout(&dialog);
        layout->addWidget(btn1);
        connect(btn1, &QPushButton::clicked, &dialog, &QDialog::close);
        connect(btn1,&QPushButton::clicked,[this](){
            game_restart();
            connect(timer,&QTimer::timeout,this,&MainWindow::update_boss);
            connect(timer_en,&QTimer::timeout,this,&MainWindow::en_rise);
            connect(timer_bb,&QTimer::timeout,this,&MainWindow::update_bigBoss);
        });
        dialog.exec();
    }
}

void MainWindow::update_role_animate(){
    currentFrame = (currentFrame+1)%4;
    //if(role_isAnimating_left){
        switch(currentFrame){
        case 0:{
            QPixmap role_left_1_pic(":/img/imgs/imgs_role/role_left/role_left_1.png");
            rolelabel->setPixmap(role_left_1_pic);
            break;
        }
        case 1:{
            QPixmap role_left_2_pic(":/img/imgs/imgs_role/role_left/role_left_2.png");
            rolelabel->setPixmap(role_left_2_pic);
            break;
        }
        case 2:{
            QPixmap role_left_3_pic(":/img/imgs/imgs_role/role_left/role_left_3.png");
            rolelabel->setPixmap(role_left_3_pic);
            break;
        }
        case 3:{
            QPixmap role_left_4_pic(":/img/imgs/imgs_role/role_left/role_left_4.png");
            rolelabel->setPixmap(role_left_4_pic);
            break;
        }
        }
    //}
    update();

    return;
}

void MainWindow::resetBigBossPath() {
    mbigboss->path.clear();
    mbigboss->currentPathIndex = 0;
}



